package com.brillilab.starter.controller.aio.lab;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.po.lab.Area;
import com.brillilab.service.core.lab.IAreaService;

/**
 * 区域API
 *
 * @author wmh
 */
@RestController
@RequestMapping("/api/area")
public class AreaController {

	 @Autowired
	 private IAreaService areaService;

    /**
     * 新增地区
     * areaName
     * parentId
     *
     * @return
     */
    @PostMapping("/add")
    public ResponseVo areaAdd(@RequestBody Area area) {


        if(StringUtils.isEmpty(area.getName()) || area.getParentId() == null){
            return ResponseVo.failure(ResultEnum.REQUEST_PARAM_LACK,"地区名称缺失！",null);
        }

        Area rs= areaService.insert(area);
        return ResponseVo.success(rs);
    }

    /**
     * 根据父级ID获取地区列表
     * {\"parentId\":\"父级ID 必填顶级地区填0 L\"}
     * @return
     */
    @PostMapping("/list")
    public ResponseVo areaList(@RequestBody  JSONObject json) {

        Long parentId=json.getLong("parentId");

        if(parentId == null){
            return ResponseVo.failure(ResultEnum.REQUEST_PARAM_LACK);
        }

        List<Area> rs = areaService.selectList(parentId);
        return ResponseVo.success(rs);
    }

    /**
     * 地区分页
     * {\"pageNum\":\"跳转到的页数 L\",\"pageSize\":\"每页记录条数 L\"}
     */
    @PostMapping("/page")
    public ResponseVo areaPage(@RequestBody JSONObject json) {

        Integer pageNum = json.getInteger("pageNum") != null ?json.getInteger("pageNum") :1;
        Integer pageSize = json.getInteger("pageSize") != null ?json.getInteger("pageSize") :10;

        PageVo<Area> rs = areaService.selectPageVo(new Page<Area>(pageNum,pageSize),null);
        return ResponseVo.success(rs);
    }

}
